package com.first.core.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class EmallStringUtil extends StringUtil
{
  private static Random randGen = null;
  private static char[] numbersAndLetters = null;
  private static Object initLock = new Object();

  public static String dateStr(int daysAfter)
  {
    return DateUtil.getDateTime("yyyy-MM-dd", 
      DateUtil.getDate(new Date(), 
      daysAfter));
  }

  public static String randomString(int length)
  {
    if (length < 1) {
      return null;
    }
    if (randGen == null) {
      synchronized (initLock) {
        if (randGen == null) {
          randGen = new Random();
          numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            .toCharArray();
        }
      }
    }
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
    }
    return new String(randBuffer);
  }

  public static String replace(String text, String with, int lastLength)
  {
    StringBuffer buf = new StringBuffer();
    if ((text == null) || (with == null) || (lastLength <= 0)) {
      return text;
    }
    if (text.length() > lastLength) {
      buf.append(text.substring(0, text.length() - lastLength));
      buf.append(with);
    } else {
      buf.append(with);
    }
    return buf.toString();
  }

  public static String abbreviateByBlank(String str, int maxWidth) {
    if (str == null) {
      return null;
    }
    if (str.length() <= maxWidth) {
      return str;
    }
    return str.substring(0, maxWidth);
  }

  public static String escapeSqlFilter(String str) {
    if (str == null)
      return null;
    int length = str.length();

    StringBuffer sql = new StringBuffer();
    for (int i = 0; i < length; i++) {
      char ch = str.charAt(i);
      switch (ch) {
      case '%':
        sql.append("\\%");
        break;
      case '?':
        sql.append("\\?");
        break;
      case '\'':
        sql.append("''");
        break;
      default:
        sql.append(ch);
      }
    }

    return sql.toString();
  }

  public static String escapeGoodsAttrFilter(String str) {
    if (str == null)
      return null;
    int length = str.length();

    StringBuffer sql = new StringBuffer();
    for (int i = 0; i < length; i++) {
      char ch = str.charAt(i);
      switch (ch) {
      case ':':
        break;
      case ';':
        break;
      default:
        sql.append(ch);
      }
    }

    return sql.toString();
  }

  public static String encodeUrl(String url) {
    return encodeUrl(url, "utf-8");
  }

  public static String encodeUrl(String url, String enc)
  {
    if (!url.contains("?")) {
      return url;
    }

    StringBuffer urlFinal = new StringBuffer();

    String urlPrefix = substringBefore(url, "?");
    urlFinal.append(urlPrefix);
    urlFinal.append("?");

    String urlParams = substringAfter(url, "?");
    String[] params = urlParams.split("&");
    int count = 1;
    for (String param : params)
    {
      param = encodeParam(param, enc);

      urlFinal.append(param);

      if (count < params.length) {
        urlFinal.append("&");
        count++;
      }
    }
    return urlFinal.toString();
  }

  public static String replaceWhiteSpace(String src)
  {
    return replaceWhiteSpace(src, " ");
  }

  public static String replaceWhiteSpace(String src, String replacement) {
    if (src == null)
      return null;
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < src.length(); i++) {
      if (Character.isWhitespace(src.charAt(i)))
        result.append(replacement);
      else {
        result.append(src.charAt(i));
      }
    }
    return result.toString();
  }

  public static void main(String[] args)
  {
  }

  public static String httpsUrlCommon(String httpUrl)
  {
    if (httpUrl == null) {
      return null;
    }
    StringBuffer sb = new StringBuffer(httpUrl);
    try {
      sb.insert(4, "s");
    } catch (Exception localException) {
      return httpUrl;
    }
    return sb.toString();
  }

  private static String encodeParam(String param, String enc) {
    try {
      return substringBefore(param, "=") + 
        "=" + 
        URLEncoder.encode(substringAfter(param, "="), 
        enc); } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
    }
    return null;
  }

  public static String leftString(String src, int size, String sep)
  {
    if (src == null) {
      return null;
    }
    if ((sep == null) || (size == 0)) {
      return src;
    }
    String result = "";
    String[] ss = StringUtil.split(src, sep);
    int i = 1;
    for (String s : ss) {
      if (i > size) {
        break;
      }
      result = result + s + ".";
      i++;
    }
    result = StringUtil.left(result, result.length() - 1);
    return result;
  }

  public static String getInterceptString(String str, int num)
  {
    if (StringUtils.isBlank(str)) {
      return null;
    }
    if (str.length() > num) {
      str = StringUtils.substring(str, 0, num - 1);
      str = str + "...";
    }
    return str;
  }

  public static String filterHtml(String message) {
    if (message == null) {
      return null;
    }
    String regEx = "<[^<|^>]+>";

    Pattern pattern = Pattern.compile(regEx);
    Matcher matcher = pattern.matcher(message);
    return matcher.replaceAll("").trim();
  }

}